package com.ysstech.reportworld.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.reportworld.entity.TableIndexed;
import com.ysstech.reportworld.enums.ReportWorldEnum;
import com.ysstech.reportworld.service.TableIndexedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 表对应索引信息 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-11-28
 */
@Controller
@Slf4j
@Api(tags = "报表世界表索引信息管理api")
@RequestMapping("/tableIndexed")
public class TableIndexedController extends BaseController {

    @Autowired
    private TableIndexedService tableIndexedService;

    /**
     * 报表世界表索引列表查询
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:table:indexed"})
    @ApiOperation(value = "获取所有报表世界表索引信息")
    @YssLog(module = "报表世界表索引管理", remark = "获取所有报表世界表索引信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(TableIndexed tableIndexed) throws Exception {
        if (null == tableIndexed || StringUtils.isBlank(tableIndexed.getTableId())) {
            throw new BusinessException("获取新增报表世界报表世界表索引信息失败，请稍后重试！");
        }
        QueryWrapper<TableIndexed> wrapper = new QueryWrapper<>();
        wrapper.eq("TABLE_ID", tableIndexed.getTableId());
        wrapper.orderByAsc("INDEXED_SEQUENCE,INDEXED_COLUMN_SEQUENCE");
        List<TableIndexed> list = tableIndexedService.list(wrapper);
        Map<String, String> map = dicValueService.getCacheDicValueMap(ReportWorldEnum.REPORT_TABLE_INDEXED.getCode());
        //状态
        Map<String, String> mapStatus = dicValueService.getCacheDicValueMap(ReportWorldEnum.REPORT_STATUS.getCode());
        if (!CollectionUtils.isEmpty(list)) {
            for (TableIndexed worldTableIndexed : list) {
                if (null == worldTableIndexed) {
                    continue;
                }
                if (!CollectionUtils.isEmpty(map) && null != worldTableIndexed.getIndexedType()) {
                    worldTableIndexed.setStrIndexedType(map.get(String.valueOf(worldTableIndexed.getIndexedType())));
                }
                //状态
                if (!CollectionUtils.isEmpty(mapStatus) && mapStatus.containsKey(String.valueOf(worldTableIndexed.getStatus()))) {
                    worldTableIndexed.setStrStatus(mapStatus.get(String.valueOf(worldTableIndexed.getStatus())));
                }
                if (StringUtils.isBlank(worldTableIndexed.getIndexedAscDesc())) {
                    worldTableIndexed.setIndexedAscDesc("0");
                }
            }
        }
        return ResultGenerator.getSuccessResult("获取所有SQL信息成功", list);
    }

    /**
     * 新增报表世界表索引的方法
     *
     * @param tableIndexed
     * @return
     */
    @YssPermissions(value = {"reportworld:table:indexed:add"})
    @ApiOperation(value = "新增报表世界表索引信息")
    @YssLog(module = "报表世界表索引管理", remark = "新增报表世界表索引信息")
    @RequestMapping(value = "/add")
    @ResponseBody
    public RestResult add(TableIndexed tableIndexed) throws Exception {
        if (null == tableIndexed || StringUtils.isBlank(tableIndexed.getTableId())) {
            throw new BusinessException("获取新增报表世界报表世界表索引信息失败，请稍后重试！");
        }
        tableIndexed.setId(DateUtil.generateUUID());
        tableIndexed.setAddTime(LocalDateTime.now());
        tableIndexed.setIndexedColumnName(tableIndexed.getIndexedColumnName().toUpperCase());
        tableIndexed.setIndexedName(tableIndexed.getIndexedName().toUpperCase());
        if ("0".equals(tableIndexed.getIndexedAscDesc())) {
            tableIndexed.setIndexedAscDesc(null);
        }
        tableIndexed.setAddUser(redisCacheManager.getLoginUserInfo().getLoginId());
        tableIndexed.setAddTime(LocalDateTime.now());
        tableIndexedService.save(tableIndexed);
        return ResultGenerator.getSuccessResult("新增报表世界表索引信息成功！");
    }

    /**
     * 修改报表世界表索引信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:table:indexed:edit"})
    @ApiOperation(value = "修改报表世界表索引信息")
    @YssLog(module = "报表世界表索引管理", remark = "修改报表世界表索引信息")
    @RequestMapping(value = "/edit")
    @ResponseBody
    public RestResult edit(TableIndexed tableIndexed) throws Exception {
        if (tableIndexed == null || StringUtils.isBlank(tableIndexed.getId())
                || StringUtils.isBlank(tableIndexed.getTableId())) {
            throw new BusinessException("获取报表世界表索引信息失败，请稍后重试！");
        }
        tableIndexed.setEditUser(redisCacheManager.getLoginUserInfo().getLoginId());
        tableIndexed.setIndexedColumnName(tableIndexed.getIndexedColumnName().toUpperCase());
        tableIndexed.setIndexedName(tableIndexed.getIndexedName().toUpperCase());
        if ("0".equals(tableIndexed.getIndexedAscDesc())) {
            tableIndexed.setIndexedAscDesc(null);
        }
        tableIndexed.setEditUser(redisCacheManager.getLoginUserInfo().getLoginId());
        tableIndexed.setEditTime(LocalDateTime.now());
        tableIndexedService.updateById(tableIndexed);
        return ResultGenerator.getSuccessResult("修改报表世界表索引信息成功！");
    }

    /**
     * 删除报表世界表索引信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:table:indexed:del"})
    @ApiOperation(value = "删除报表世界表索引信息")
    @YssLog(module = "报表世界表索引管理", remark = "删除报表世界表索引信息")
    @RequestMapping(value = "/del")
    @ResponseBody
    public RestResult del(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除报表世界表索引信息失败，请稍后重试！");
        }
        List<String> listId = JSONArray.parseArray(ids, String.class);
        tableIndexedService.removeByIds(listId);
        return ResultGenerator.getSuccessResult("删除报表世界表索引信息成功！");
    }

    /**
     * 检查报表世界表索引信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:table:indexed"})
    @ApiOperation(value = "检查报表世界表索引信息")
    @YssLog(module = "报表世界表索引管理", remark = "检查报表世界表索引信息")
    @RequestMapping(value = "/checkTableIndexed")
    @ResponseBody
    public RestResult checkTableIndexed(TableIndexed tableIndexed) throws Exception {
        if (tableIndexed == null || StringUtils.isBlank(tableIndexed.getIndexedName())
                || StringUtils.isBlank(tableIndexed.getTableId())
                || StringUtils.isBlank(tableIndexed.getIndexedColumnName())) {
            return ResultGenerator.getSuccessResult("检查表索引信息成功", true);
        }
        QueryWrapper<TableIndexed> wrapper = new QueryWrapper<>();
        wrapper.eq("TABLE_ID", tableIndexed.getTableId());
        wrapper.eq("INDEXED_NAME", tableIndexed.getIndexedName());
        wrapper.eq("INDEXED_COLUMN_NAME", tableIndexed.getIndexedColumnName());
        int count = tableIndexedService.count(wrapper);
        if (count > 0) { //返回true表示已经存在
            return ResultGenerator.getSuccessResult("检查表索引信息成功", false);
        } else {
            return ResultGenerator.getSuccessResult("检查表索引信息成功", true);
        }
    }


    /**
     * 保存顺序
     *
     * @param tableIndexed
     * @return
     */
    @YssPermissions(value = {"reportworld:table:indexed:sequence"})
    @ApiOperation(value = "批量修改索引顺序信息")
    @YssLog(module = "报表世界表索引管理", remark = "批量修改索引顺序信息")
    @RequestMapping(value = "/updateTableIndexedSequence")
    @ResponseBody
    public RestResult updateTableIndexedSequence(String tableIndexed) throws Exception {
        if (StringUtils.isBlank(tableIndexed)) {
            throw new BusinessException("获取表索引信息失败，请稍后重试！");
        }
        List<TableIndexed> listTableDetail = JSONArray.parseArray(tableIndexed, TableIndexed.class);
        if (CollectionUtils.isEmpty(listTableDetail)) {
            throw new BusinessException("获取表索引信息失败，请稍后重试！");
        }
        tableIndexedService.updateBatchById(listTableDetail);
        return ResultGenerator.getSuccessResult("批量修表索引顺序值信息成功");
    }


}
